/*!
// Testweb v0.4.0
// http://or-change.cn
// Copyright 2014 OrChange Inc. All rights reserved.
// Licensed under the GPL License.
*/
/*jslint vars: true, forin: true, plusplus: true */
/*global define: false */
/**
 * DelayStep extend from Step. This kind of Step will
 * run after a period of time(delay).
 *
 * @module DelayStep
 * @requires Step
 * @requires OC
 * @example
 *     var DelayStep = require("./oc.delaystep"); //How to require DelayStep module.
 */
define(function (require, exports, module) {
	'use strict';
	var DelayStep, Step = require("./step"),
		OC = require("lib/core/oc");
	/**
	 * DelayStep extend from Step. This kind of Step will
	 * run after a period of time(delay).
	 *
	 * @class DelayStep
	 * @extends Step
	 * @constructor
	 */
	DelayStep = function DelayStep(options) {
		/**
		 * If no param input, make time be DEFAULT_DELAY .
		 * @property delay
		 * @type number
		 */
		this.delay = options.delay || OC.cfg("DEFAULT_DELAY");
		/**
		 * Cache the ID of timeout timer (id of setTimeout()).
		 * @property timer_id
		 * @type number
		 */
		this.timer_id = 0;

		Step.call(this, options);
	};
	DelayStep.prototype = new Step();
	DelayStep.prototype.constructor = DelayStep;
	DelayStep.prototype.run = function (callback) {
		var that = this;
		that.timer_id = setTimeout(
			function () {
				if (that.$run()) {
					if (typeof callback === "function") {
						callback.call(that.$process, 1);
					}
					that.$process.$core();
				} else {
					if (typeof callback === "function") {
						callback.call(that.$process, 0);
					}
				}
			},
			// Control the real delay by DELAY_RATIO.
			this.delay * (OC.cfg("DELAY_RATIO_MODE") ? OC.cfg("DELAY_RATIO") : 1.0)
		);
	};
	DelayStep.prototype.abort = function () {
		var timer_id = this.timer_id;
		clearTimeout(timer_id);
	};
	return DelayStep;
});
